/* Copyright 2012 Oliver Katz

    This file is part of Theta.

    Theta is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#ifndef __THETA_PARSING_H
#define __THETA_PARSING_H

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>

#include <fstream>
#include <streambuf>

#include <unistd.h>

#include "frame.h"

namespace parsing
{
	using namespace std;

	class ParsingErrorBuffer
	{
	private:
		string file;
		int linenum;
		vector<string> errors;
		vector<string> warnings;

	public:
		ParsingErrorBuffer();
		void setFileName(string f);
		void setLineNumber(int n);
		void error(string msg, string chunk, string line);
		void warning(string msg, string chunk, string line);
		void dump();
	};

	vector<string> tokenize(string s, ParsingErrorBuffer &b);

	class Clause
	{
	private:
		string name;
		map<string, vector<string> > cfg;
		vector<string> args;
		Frame frame;

	public:
		Clause();
		Clause(string n);
		void addArgument(string a);
		int getArgumentSize();
		void addCfgLine(vector<string> cl);
		void addFrame(Frame f);
		string display();

		vector<string> getPlugins();
		vector<string> getCfg(string plugin);
		Frame getFrame();
	};

	class Parser
	{
	private:
		ParsingErrorBuffer pebble; // hehe :)
		map<string, Clause> clauses;

		int branches(string s);

		vector<string> splitLines(string page);
		vector<string> shuntingYard(vector<string> toks, string line);
		Frame treeify(vector<string> rpn, string line);

	public:
		bool isFunction(string s);
		bool isSymbol(string s);
		bool isValue(string s);
		bool isSeparator(string s);
		bool isLeftBracket(string s);
		bool isRightBracket(string s);
		bool isOperator(string s);

		Parser();
		string readFile(string path);
		map<string, Clause> parse(string page);
	};
}

#endif